<html>
<head><meta charset="utf-8"><title>RFC: &quot;UB during const&quot; · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html">RFC: &quot;UB during const&quot;</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="201883547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201883547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201883547">(Jun 24 2020 at 18:12)</a>:</h4>
<p>So would that be a reasonable next step in the unsafe+const space for us -- to write an RFC that specifies what can and cannot happen from UB during const-eval?</p>



<a name="201883568"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201883568" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201883568">(Jun 24 2020 at 18:12)</a>:</h4>
<p>feels to me like that (and the corresponding nomicon chapter) should come before we even tackle unconst</p>



<a name="201883583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201883583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201883583">(Jun 24 2020 at 18:12)</a>:</h4>
<p>and then a very "cheap" solution for unconst would be to say it is UB, period^^</p>



<a name="201883674"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201883674" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201883674">(Jun 24 2020 at 18:14)</a>:</h4>
<p><span class="user-group-mention" data-user-group-id="1916">@WG-const-eval</span> ^</p>



<a name="201883747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201883747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201883747">(Jun 24 2020 at 18:14)</a>:</h4>
<p>Are we concerned that, by punting and saying unconst = UB, we'll have people writing unconst code that happens to work?</p>



<a name="201883912"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201883912" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201883912">(Jun 24 2020 at 18:15)</a>:</h4>
<p>In other words, is it feasible/desirable to break unconst operations during CTFE while we're in a transitional period where <code>unsafe</code> operations are stable but unconst operations are technically UB?</p>



<a name="201884123"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201884123" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201884123">(Jun 24 2020 at 18:17)</a>:</h4>
<p><span class="user-mention silent" data-user-id="118594">ecstatic-morse</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/RFC.3A.20.22UB.20during.20const.22/near/201883747">said</a>:</p>
<blockquote>
<p>Are we concerned that, by punting and saying unconst = UB, we'll have people writing unconst code that happens to work?</p>
</blockquote>
<p>well, we detect unconst when it happens</p>



<a name="201884189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201884189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201884189">(Jun 24 2020 at 18:18)</a>:</h4>
<p>so I think my main concern is that "unconst = UB" is weaker than what we actually implement, and likely weaker than any reasonable implementation</p>



<a name="201884200"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201884200" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201884200">(Jun 24 2020 at 18:18)</a>:</h4>
<p>OTOH it is very easy to explain I think</p>



<a name="201884274"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201884274" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201884274">(Jun 24 2020 at 18:19)</a>:</h4>
<p><span class="user-mention silent" data-user-id="118594">ecstatic-morse</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/RFC.3A.20.22UB.20during.20const.22/near/201883912">said</a>:</p>
<blockquote>
<p>In other words, is it feasible/desirable to break unconst operations during CTFE while we're in a transitional period where <code>unsafe</code> operations are stable but unconst operations are technically UB?</p>
</blockquote>
<p>I am not sure what you concern is here? do you have an example?</p>



<a name="201884338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201884338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201884338">(Jun 24 2020 at 18:20)</a>:</h4>
<p>Not immediately.</p>



<a name="201884769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201884769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201884769">(Jun 24 2020 at 18:23)</a>:</h4>
<p>I guess it doesn't really affect the RFC you want to do. Only once we start const-stabilizing various <code>unsafe</code> operations in the future. I'll try to come up with a coherent example before then.</p>



<a name="201897992"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201897992" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201897992">(Jun 24 2020 at 20:15)</a>:</h4>
<p>Trying to write down a response here i came to the conclusion that unconst and UB behaviour are orthogonal topics. So yea, let's do an RFC defining the boundaries of UB during const eval. After that we can explain unconst ops. Then comes the part where they overlap: even if we forbid unconst ops statically, you can write unsafe code that works around that (e.g. by transmuting a reference to a usize)</p>



<a name="201937021"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201937021" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201937021">(Jun 25 2020 at 06:45)</a>:</h4>
<blockquote>
<p>even if we forbid unconst ops statically, you can write unsafe code that works around that (e.g. by transmuting a reference to a usize)</p>
</blockquote>
<p>Right. So this means we at least have to make unconst ops UB before we stabilize any unsafe op.</p>
<p>I am not convinced it is worth doing more for unconst.</p>



<a name="201937096"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201937096" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201937096">(Jun 25 2020 at 06:46)</a>:</h4>
<p>Also I feel none of this should block <code>transmute</code> in <code>const</code> (not <code>const fn</code>)</p>



<a name="201937184"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201937184" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201937184">(Jun 25 2020 at 06:49)</a>:</h4>
<p>Here's <a href="https://github.com/rust-lang/rust/issues/71499#issuecomment-649270784">another kind of UB</a> that we cannot reliably check for during CTFE (besides MIR optimizations exploiting UB)</p>



<a name="201937482"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201937482" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201937482">(Jun 25 2020 at 06:56)</a>:</h4>
<p>hmm didn't I have a PR working on the transmute in <code>const</code>? XD let me check my PRs</p>



<a name="201937499"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201937499" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201937499">(Jun 25 2020 at 06:57)</a>:</h4>
<p>ah yes <a href="https://github.com/rust-lang/rust/pull/72920">https://github.com/rust-lang/rust/pull/72920</a></p>



<a name="201937564"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201937564" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201937564">(Jun 25 2020 at 06:58)</a>:</h4>
<p>yea, <code>transmute</code> in <code>const</code> isn't blocked on anything. We're just waiting for the rest of T-lang to sign off on the stabilization</p>



<a name="201958618"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201958618" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201958618">(Jun 25 2020 at 11:49)</a>:</h4>
<p>nice :)</p>



<a name="201959154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201959154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201959154">(Jun 25 2020 at 11:56)</a>:</h4>
<p>so coming back to "orthogonal", I am not sure I agree with that. As you said, once we have unsafe code it can cause unconst things. and conversely, saying "its UB" is one option for specifying unconst ops.</p>



<a name="201964237"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201964237" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201964237">(Jun 25 2020 at 12:52)</a>:</h4>
<p>sure, I just meant the "what is UB" and "what is unconst" questions are independent things to explain, and starting them out intermingled will cause confusion</p>



<a name="201964337"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201964337" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201964337">(Jun 25 2020 at 12:53)</a>:</h4>
<p>A single RFC could explain all steps, but keeping these separate while introducing them helps communicate the point of each imo</p>



<a name="201965113"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201965113" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201965113">(Jun 25 2020 at 12:59)</a>:</h4>
<p>I agree they should be separate RFCs</p>



<a name="201965167"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201965167" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201965167">(Jun 25 2020 at 13:00)</a>:</h4>
<p>I feel there is a dependency here where it helps to have the UB RFC first</p>



<a name="201965775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201965775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201965775">(Jun 25 2020 at 13:05)</a>:</h4>
<p>seems reasonable to me</p>



<a name="201965786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/RFC%3A%20%22UB%20during%20const%22/near/201965786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/RFC.3A.20.22UB.20during.20const.22.html#201965786">(Jun 25 2020 at 13:05)</a>:</h4>
<p>it's the one without any prerequisites</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>